/*
 * Copyright (c) 2023-2025 elsfs Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.elsfs.cloud.starter.excel.handler;

import cn.idev.excel.ExcelWriter;
import cn.idev.excel.FastExcel;
import cn.idev.excel.converters.Converter;
import cn.idev.excel.write.metadata.WriteSheet;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import org.elsfs.cloud.starter.excel.annotation.ResponseExcel;
import org.elsfs.cloud.starter.excel.annotation.Sheet;
import org.elsfs.cloud.starter.excel.configuration.ExcelConfigProperties;
import org.elsfs.cloud.starter.excel.enhance.WriterBuilderEnhancer;
import org.elsfs.cloud.starter.excel.kit.ExcelException;
import org.springframework.util.CollectionUtils;

/**
 * 多Sheet 处理器
 *
 * @author zeng
 */
public class ManySheetWriteHandler extends AbstractSheetWriteHandler {

  public ManySheetWriteHandler(
      ExcelConfigProperties configProperties,
      List<Converter<?>> converterProvider,
      WriterBuilderEnhancer excelWriterBuilderEnhance) {
    super(configProperties, converterProvider, excelWriterBuilderEnhance);
  }

  /**
   * 当且仅当List不为空且List中的元素也是List 才返回true
   *
   * @param obj 返回对象
   * @return boolean
   */
  @Override
  public boolean support(Object obj) {
    if (obj instanceof List) {
      List<?> objList = (List<?>) obj;
      return !objList.isEmpty() && objList.get(0) instanceof List;
    } else {
      throw new ExcelException("@ResponseExcel 返回值必须为List类型");
    }
  }

  @Override
  public void write(Object obj, HttpServletResponse response, ResponseExcel responseExcel) {
    List<?> objList = (List<?>) obj;
    ExcelWriter excelWriter = getExcelWriter(response, responseExcel);

    Sheet[] sheets = responseExcel.sheets();
    WriteSheet sheet;
    for (int i = 0; i < sheets.length; i++) {
      List<?> eleList = (List<?>) objList.get(i);

      if (CollectionUtils.isEmpty(eleList)) {
        sheet = FastExcel.writerSheet(responseExcel.sheets()[i].sheetName()).build();
      } else {
        // 有模板则不指定sheet名
        Class<?> dataClass = eleList.get(0).getClass();
        sheet =
            this.sheet(
                responseExcel.sheets()[i],
                dataClass,
                responseExcel.template(),
                responseExcel.headGenerator());
      }

      // 填充 sheet
      if (responseExcel.fill()) {
        excelWriter.fill(eleList, sheet);
      } else {
        // 写入sheet
        excelWriter.write(eleList, sheet);
      }
    }
    excelWriter.finish();
  }
}
